import android.view.LayoutInflater
import android.webkit.WebView
import android.widget.TextView
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.viewinterop.AndroidView
import com.cy.mycomposedemo.R

@Composable
fun DisposableEffectView() {
    Row(
        Modifier.fillMaxSize(),
        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.CenterVertically
    ) {
        val context = LocalContext.current
        val webView: WebView = WebView(context).apply {
//            id = R.id.webView
            loadUrl("https://blog.csdn.net/shop_and_sleep?spm=1010.2135.3001.5343")
        }


        AndroidView(
            factory = { _ ->
                webView
            }, modifier = Modifier.fillMaxSize()
        )
    }

}

/**
 * Compose中加载AndroidView
 */
//@Composable
//fun LoadAndroidView(name: String) {
//    var tvTittle: TextView? = null
//    AndroidView(factory = {
//        //加载AndroidView布局。
//        LayoutInflater.from(it).inflate(R.layout.activity_main, null).apply {
//            tvTittle = findViewById(R.id.tvTittle)
//        }
//    }) {
//        //更新UI数据
//        tvTittle?.text = name
//    }
//}

@Composable
fun APage() {
    val timeData = remember {
        mutableStateOf(0L)
    }
    Column(
        Modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center
    ) {
        //这里在AndroidView的factory构建TextView
        AndroidView(factory = {
            TextView(it).apply {
                text = System.currentTimeMillis().toString()
                textSize = 40f
                setTextColor(android.graphics.Color.WHITE)
            }
        }, modifier = Modifier.background(color = Color.Gray)//这边依然可以使用compose的modifier对View进行配置

            , update = { //update是用来更新AndroidView里的数据的
                it.text = timeData.value.toString()
            })

        //这里举例通过Button更新AndroidView里的数据
        Button(onClick = {
            timeData.value = System.currentTimeMillis()
        }) {
            Text(text = "更新时间")
        }
    }
}